package com.example.demo.controller.dynamic;

import com.example.demo.bean.ErrorCode;
import com.example.demo.bean.JsonData;
import com.example.demo.bean.ThisSystemException;
import com.example.demo.bean.request.ReleaseRequest;
import com.example.demo.bean.response.DynamicListResponse;
import com.example.demo.bean.response.LoginResponse;
import com.example.demo.repository.DynamicRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.table.DynamicTable;
import com.example.demo.table.UserTable;
import com.example.demo.utli.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import io.swagger.annotations.Api;


@RestController
@Api(description = "动态" )
@RequestMapping(value = "/dynamic")
public class DynamicController {

    @Autowired
    DynamicRepository mDynamicRepository;

    @Autowired
    UserRepository mUserRepository;

    @Autowired
    RedisUtil mRedisUtil;

    /**
     * 发布动态
     */
    @PostMapping(value = "release")
    public JsonData release(@Valid @RequestBody ReleaseRequest releaseRequest,HttpServletRequest httpServletRequest) {

        Optional<UserTable> user = mUserRepository.findById(releaseRequest.getUserID());
        if (!user.isPresent()) {
            throw new ThisSystemException(ErrorCode.NO_CORRESPONDING_ID_ACCOUNT_WAS_FOUND.getText()
                    , ErrorCode.NO_CORRESPONDING_ID_ACCOUNT_WAS_FOUND.getStatus());
        }



        mDynamicRepository.save(new DynamicTable(releaseRequest.getUserID(), new Date(System.currentTimeMillis()),
                releaseRequest.getPictureSet(), releaseRequest.getLabel(), releaseRequest.getContent()));
        return JsonData.success("发布成功");
    }

    /**
     * 获取自己的动态
     */
    @PostMapping(value = "getMyDynamicList")
    public JsonData getMyDynamicList(HttpServletRequest request) {
        LoginResponse user = (LoginResponse) request.getSession().getAttribute("user");
        if (null == user) {
            throw new ThisSystemException(ErrorCode.PLEASE_LOG_IN_FIRST.getText(), ErrorCode.PLEASE_LOG_IN_FIRST.getStatus());
        }
        List<DynamicListResponse> dynamicListResponses = mDynamicRepository.selectDynamicList(user.getUserID());
        return JsonData.success(dynamicListResponses);
    }

    /**
     * 获取他人的动态
     */
    @PostMapping(value = "getDynamicList")
    public JsonData getDynamicList(@RequestParam(value = "userID") Integer userID, HttpServletRequest request) {
        LoginResponse user = (LoginResponse) request.getSession().getAttribute("user");
        if (null == user) {
            throw new ThisSystemException(ErrorCode.PLEASE_LOG_IN_FIRST.getText(), ErrorCode.PLEASE_LOG_IN_FIRST.getStatus());
        }
        List<DynamicListResponse> dynamicListResponses = mDynamicRepository.selectDynamicList(userID);
        return JsonData.success(dynamicListResponses);
    }

    /**
     * 获取所有动态
     */
    @PostMapping(value = "getAllDynamicList")
    public JsonData getAllDynamicList(HttpServletRequest request) {
        LoginResponse user = (LoginResponse) request.getSession().getAttribute("user");
        if (null == user) {
            throw new ThisSystemException(ErrorCode.PLEASE_LOG_IN_FIRST.getText(), ErrorCode.PLEASE_LOG_IN_FIRST.getStatus());
        }
        List<DynamicListResponse> dynamicListResponses = mDynamicRepository.selectAllDynamicList();
        return JsonData.success(dynamicListResponses);
    }




}
